package controles;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import modeles.DBManager;

import fenetres.Accueil;
import fenetres.AuthentificationFrame;

public class ConnexionAction implements ActionListener{
	AuthentificationFrame authentFrame ;
	Accueil accueil;
	/*public ConnexionAction(Accueil _accueil){
		accueil = _accueil;
		authentFrame = _accueil.authent;
	}*/
	
	public ConnexionAction(AuthentificationFrame authent){
		
		authentFrame = authent;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		if(arg0.getSource() == authentFrame.connexBut){
			String login = authentFrame.loginTxt.getText();
			String passwd = authentFrame.mdpTxt.getText();
			
			DBManager dbManager = new DBManager();
			authentFrame.userConnected.setLogin(login);
			authentFrame.userConnected.setPasswd(passwd);
			authentFrame.setUserConnected(authentFrame.userConnected.existUser(dbManager));
			
			
			if(authentFrame.userConnected == null){
				JOptionPane.showMessageDialog(null, "Login ou mot de passe incorrect !!!","Erreur",JOptionPane.ERROR_MESSAGE);
				authentFrame.loginTxt.setText("");
				authentFrame.mdpTxt.setText("");
			}
			else{
				authentFrame.dispose();
				String message = "Vous êtes connectés entant que "+authentFrame.userConnected.getNom()+"";
				JOptionPane.showMessageDialog(null,message ,"Confirmation",JOptionPane.INFORMATION_MESSAGE);
			}
		}
		
		if(arg0.getSource() == authentFrame.cancelBut){
			authentFrame.setVisible(false);
		}
	}

}
